package com.example.demo.controller;

import com.example.demo.entity.User;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * 测试linux curl
 */
@RestController
@RequestMapping("/curl")
public class CurlController {

    /**
     * 测试get多参数
     * postman里可以直接访问  http://localhost:8080/curl/multiParam?name=xcw&age=24&jsonParam={"code":"a","value":123}
     * linux里 
     * 1.如果有中文必须encode  
     * 2.&需要转义写为\&  
     * 3.json格式的参数不encode会有特殊输出，输出两次，code，value拆开来输出  eg:name:xcw,age:24,jsonParam:code:aname:xcw,age:24,jsonParam:value:123
     * 4.最好直接将postman里可用的链接使用线上工具转码下 类似网站：https://www.sojson.com/encodeurl.html
     * 结果url：curl -X GET http://localhost:9000/web/curl/multiParam?name=%E5%B0%8F%E4%BB%93%E5%94%AF\&age=24\&jsonParam=%7B%22code%22:%22a%22,%22value%22:123%7D
     * 转义前：http://localhost:9000/web/curl/multiParam?name=小仓唯&age=24&jsonParam={"code":"a","value":123}
     * @param name
     * @param age
     * @param jsonParam
     * @return
     */
    @GetMapping("/multiParam")
    public String multiParam(String name, Integer age, String jsonParam){
        String a = StringUtils.isEmpty(name) ? null : name;
        String b = age==null ? null : age.toString();
        String c = StringUtils.isEmpty(jsonParam) ? null : jsonParam;
        String result = String.format("name:%s,age:%s,jsonParam:%s", a, b, c);
        return result;
    }

    /**
     * post测试
     * curl -H "Accept: application/json" -H "Content-type: application/json" -d '{"name":"小仓唯","age":"24"}' -X POST http://localhost:9000/web/curl/postTest
     * 可以直接传中文
     * @param user
     * @return
     */
    @PostMapping("/postTest")
    public String postTest(@RequestBody User user){
        return user.toString();
    }
}
